<?php

namespace app\admin\model;

use think\Model;

/**
 * Merchant
 */
class Merchant extends Model
{
    // 表名
    protected $name = 'merchant';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 字段类型转换
    // protected $type = [
    //     'timestamp' => 'timestamp:Y-m-d H:i:s',
    // ];
 
    protected $createTime = 'timestamp'; // 自定义创建时间字段名
 

    public function market(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Market::class, 'market_id', 'id');
    }


    public function parameter(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\cans\Parameter::class, 'type', 'id');
    }
    
    public function getDeviceAttr($value, $row): array
    {
        return [
            'count'=>\app\admin\model\Device::where('merchant_id', $row['id'])->count()
        ];
    }
    protected static function onBeforeUpdate($user)
    {
        unset($user->device);
    }

}